﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;

public class EditorCamera : MonoBehaviour
{

	public static List<ListKey> keyList = new List<ListKey>();
	public float cameraSensitivity = 90;
	public float moveSpeed = 10;
	public float fastMoveFactor = 3;
	public float whellSpeed = 20;
	private float rotationX = 0.0f;
	private float rotationY = 0.0f;

	public Texture2D rotateCursor;
	public Texture2D panCursor;
	private int cursorSizeX = 25;  // set to width of your cursor texture
	private int cursorSizeY = 25;  // set to height of your cursor texture
	private bool showRotate = false;
	private bool showPan = false;
	public GUISkin myskin;
	private float timer = 0.0f;
	private float maxTimer = 100.0f;
	private bool keyState = false;
	public GameObject target;
	public static List<GameObject> pointList = new List<GameObject>();
	public GameObject BrowsersParent;
	private bool playAnim=false;
	private int currentKey = 0;
	private int index = 1;
	private Browser Browser;
	private float timePlay;
	private float timeOver = 0;
	private float timePlayKey;
	private float timeOverKey = 0;
	private float t = 0;
	private int keyIndex = 1;
	private bool playFollow = false;
	private Rect timerRect = new Rect(0,Screen.height-60,1024,25);
	void Awake() // Runs once before Start, best used to get refrences to other scripts.
	{
		Browser = BrowsersParent.GetComponent<Browser>();
	}
	void Start ()
	{
	}
	void OnGUI(){
		GUI.skin = myskin;
		if(showRotate){
			Screen.showCursor = false;
			GUI.DrawTexture (new Rect(Input.mousePosition.x-cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 , cursorSizeX, cursorSizeY),rotateCursor);
		}
		if(showPan){
			Screen.showCursor = false;
			GUI.DrawTexture (new Rect(Input.mousePosition.x-cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 , cursorSizeX, cursorSizeY),panCursor);
		}
		GUI.Label(new Rect(0,0,Screen.width,22),"","MenuTop");
		GUI.Label(new Rect(0,22,Screen.width,22),"","MenuTop1");
		GUI.Label(new Rect(1024,Screen.height-60,Screen.width,60),"","MenuBottom1");
		GUI.Label(new Rect(0,Screen.height-35,1024,35),"","MenuBottom1");

		maxTimer = GUI.HorizontalSlider(new Rect(1080,Screen.height-57.5f,Screen.width-1080,25),maxTimer,100.0f,5000.0f);
		GUI.Label(new Rect(2.5f+timer*990/maxTimer,Screen.height-30,30,25),"","ShowKey");
		if(GUI.Button(new Rect(1150,Screen.height-30,50,27.5f),"Clear","SetButtonKey"))
		{
			if(keyList.Count!=0)
			{
				keyList.Clear();
				keyIndex = 1;
			}
		}
		if(!keyState)
		{
			GUI.Button(new Rect(2.5f,0,50,22),"Save","MenuButton");
			GUI.Button(new Rect(55,0,50,22),"Load","MenuButton");
			GUI.Label(new Rect(0,44,Screen.width,Screen.height-104),"","MenuCenter");
			GUI.Label(new Rect(0,Screen.height-60,1024,25),"","MenuBottom");
			GUI.Button(new Rect(1027.5f,Screen.height-55,50,50),"","SetKey");
			timer = GUI.HorizontalSlider(timerRect,timer,0.0f,maxTimer);
			GUI.Label(new Rect(timer*904/maxTimer,Screen.height-58,120,25),string.Format("{0:####0}",timer)+"/"+string.Format("{0:####0}",maxTimer));
			if(GUI.Button(new Rect(1080,Screen.height-30,70,27.5f),"Set Key","SetButtonKey"))
			{
				keyState = true;
			}
		}
		if(keyState)
		{
			if(GUI.Button(new Rect(107.5f,0,50,22),"Play","MenuButton"))
			{
				playAnim = true;
				timer = keyList[0].time;
			}

			GUI.Label(new Rect(0,44,Screen.width,Screen.height-104),"","MenuCenterKeyState");
			GUI.Label(new Rect(0,Screen.height-60,1024,25),"","MenuBottomKeyState");
			timer = GUI.HorizontalSlider(timerRect,timer,0.0f,maxTimer);
			GUI.Label(new Rect(timer*904/maxTimer,Screen.height-58,120,25),string.Format("{0:####0}",timer)+"/"+string.Format("{0:####0}",maxTimer));
			if(GUI.Button(new Rect(2.5f,0,50,22),"Save","MenuButton"))
			{
				var FileName = Application.dataPath + "/TempSaveFile";
				var Sw = new StreamWriter(FileName);
				Sw.WriteLine("<AnimationKey>");
				for(int i =0;i<keyList.Count;i++)
				{
					var t = string.Format("{0:###0.00000}",keyList[i].time);
					var p = string.Format("{0:###0.00000}",keyList[i].pos.x)+","+string.Format("{0:###0.00000}",keyList[i].pos.y)+","+string.Format("{0:###0.00000}",keyList[i].pos.z);
					var r = string.Format("{0:###0.00000}",keyList[i].rot.x)+","+string.Format("{0:###0.00000}",keyList[i].rot.y)+","+string.Format("{0:###0.00000}",keyList[i].rot.z);

					Sw.WriteLine("<"+"\""+FileName.Substring(Application.dataPath.Length,FileName.Length)+"\""+ " t="+"\""+t+"\""+" p="+"\""+p+"\""+" r="+"\""+r+"\""+" />");
					
				}
				Sw.WriteLine("</AnimationKey>");
				Sw.Close();
				Browser.SaveFile( FileName, Application.dataPath+"/Demo Directory Tree");

			}
			if(GUI.Button(new Rect(55,0,50,22),"Load","MenuButton"))
			{
				Browser.OpenFile( Application.dataPath+"/Demo Directory Tree");
			}
			if(GUI.Button(new Rect(1027.5f,Screen.height-55,50,50),"","SetKey"))
			{
				print ("Create animation point");
				if(Gizmo.Visible)
				{
					CreateKey();
				}
			}
			if(Gizmo.Visible){
				for(int i=0;i<keyList.Count;i++)
				{
					GUI.Label(new Rect(2.5f+keyList[i].time*990/maxTimer,Screen.height-30,30,25),"","KeyCode");
				}
			}
			if(GUI.Button(new Rect(1080,Screen.height-30,70,27.5f),"Set Key","SetButtonKeyState"))
			{
				keyState = false;
			}
		}

		if(maxTimer<=135)
		{
			for(int i=0;i<=(maxTimer)/5;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*5/maxTimer),Screen.height-22,30,25),""+i*5);
			}
		}
		else if(maxTimer<=270)
		{
			for(int i=0;i<=(maxTimer)/10;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*10/maxTimer),Screen.height-22,30,25),""+i*10);
			}
		}
		else if(maxTimer<=540)
		{
			for(int i=0;i<=(maxTimer)/20;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*20/maxTimer),Screen.height-22,30,25),""+i*20);
			}
		}
		else if(maxTimer<=1350)
		{
			for(int i=0;i<=(maxTimer)/50;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*50/maxTimer),Screen.height-22,30,25),""+i*50);
			}
		}
		else if(maxTimer<=2700)
		{
			for(int i=0;i<=(maxTimer)/100;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*100/maxTimer),Screen.height-22,30,25),""+i*100);
			}
		}
		else if(maxTimer<=5400)
		{
			for(int i=0;i<=(maxTimer)/200;i++)
			{
				GUI.Label(new Rect(2.5f+i*(990*200/maxTimer),Screen.height-22,30,25),""+i*200);
			}
		}
	}
	void SaveAnimationXML(){

	}

	void CreateKey(){
		ListKey key = new ListKey();
		key.time = timer;
		key.pos = target.transform.position;
		key.rot = target.transform.eulerAngles;
		keyList.Add(key);
		keyList.Sort(Comparision);
	}
	int Comparision(ListKey k1, ListKey k2)
	{
		float t1 = k1.time;
		float t2 = k2.time;
		if(t1 > t2)
		{
			return 1;
		}
		else if(t1<t2)
		{
			return -1;
		}
		return 0;
	}
	void AutoCreateKey(){

	}

	void Update ()
	{
		if(playAnim)
		{
			if(keyList.Count!=0)
			{	
				t+= Time.deltaTime;
				timer = keyList[0].time + t;
				Debug.Log (timer);
				if(index == keyList.Count)
				{
					index = 1;
					t = 0;
					playAnim = false;
					return;
				}
				else
				{
					timePlay = keyList[index].time - keyList[index-1].time;
					timeOver += Time.deltaTime;
					if(timePlay > timeOver)
					{
						target.transform.position = Vector3.Lerp(keyList[index-1].pos,keyList[index].pos,timeOver/timePlay);
						target.transform.eulerAngles = Vector3.Lerp(keyList[index-1].rot,keyList[index].rot,timeOver/timePlay);
					}
					else
					{
						index++;
						Debug.Log (keyList.Count);
						timeOver = 0;
					}
				}
			}
		}

		if(Input.GetMouseButtonDown(0))
		{
			Debug.Log (Input.mousePosition);
			if(timerRect.Contains(new Vector2(Input.mousePosition.x,Screen.height-Input.mousePosition.y)))
			{
				playAnim = false;
				playFollow = true;
			}
		}
		if(Input.GetMouseButtonUp(0))
		{
			playFollow =false;
		}
		if(playFollow)
		{
			if(keyList.Count > 1)
			{
				for(int i=0;i<keyList.Count - 1;i++)
				{
					if((timer-keyList[i].time)>=0)
						keyIndex =i+1;
					else
						i = keyList.Count;
				}
				if(keyIndex<keyList.Count)
				{
					timePlayKey = keyList[keyIndex].time - keyList[keyIndex-1].time;
					timeOverKey = timer - keyList[keyIndex-1].time;
					Debug.Log (timer+"Timer "+ timeOverKey + " TIME PLAY " + timePlayKey +" Key"+keyIndex);
					if(timePlayKey>timeOverKey)
					{
						target.transform.position = Vector3.Lerp(keyList[keyIndex-1].pos,keyList[keyIndex].pos,timeOverKey/timePlayKey);
						target.transform.eulerAngles = Vector3.Lerp(keyList[keyIndex-1].rot,keyList[keyIndex].rot,timeOverKey/timePlayKey);
					}
					else{
						keyIndex++;
						timeOverKey = 0;
						Debug.Log (timer+" keyIndex " +keyIndex+" Time over :"+timeOverKey);
					}
				}
			}
		}
		if(timer>maxTimer)
			timer = maxTimer;
		if(Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
		{
			showPan = false;
			showRotate = false;
			Screen.showCursor = true;
		}
		if(Input.GetMouseButton(1))
		{
			showRotate = true;
			rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
			rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
			rotationY = Mathf.Clamp (rotationY, -90, 90);
		}
		if(Input.GetMouseButton(2))
		{
			showPan = true;
			transform.position -= transform.up * (moveSpeed * fastMoveFactor) * Input.GetAxis("Mouse Y") * Time.deltaTime;
			transform.position -= transform.right * (moveSpeed * fastMoveFactor) * Input.GetAxis("Mouse X") * Time.deltaTime;
		}
		transform.position += transform.forward * whellSpeed * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime;

		transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
		transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
		
		transform.position += transform.forward * moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
		transform.position += transform.right * moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;

		if (Input.GetKey (KeyCode.Q)) {transform.position += transform.up * moveSpeed * Time.deltaTime;}
		if (Input.GetKey (KeyCode.E)) {transform.position -= transform.up * moveSpeed * Time.deltaTime;}
	}
}